package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfMpExternalPurchase;
import com.yuandian.api.management.entity.ConfMpInternalPurchase;
import com.yuandian.api.management.po.ConfMpPurchaseParams;
import com.yuandian.api.management.vo.ConfMpExternalPurchaseExcelVo;
import com.yuandian.api.management.vo.ConfMpInternalPurchaseExcelVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.excel.annotation.RequestExcel;
import com.yuandian.bpm.common.excel.annotation.ResponseExcel;
import com.yuandian.bpm.common.excel.vo.ErrorMessage;
import com.yuandian.management.service.ConfMpExternalPurchaseService;
import com.yuandian.management.service.ConfMpInternalPurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;

/**
 * 说明：中台监控-订购单配置controller
 * @Author zfsn
 * @Date 2023/4/20 13:31
 */
@Api(tags = "中台监控-订购单配置")
@RestController
@RequestMapping("/mp/purchase")
public class ConfMpPurchaseController {

	@Autowired
	private ConfMpInternalPurchaseService confMpInternalPurchaseService;

	@Autowired
	private ConfMpExternalPurchaseService confMpExternalPurchaseService;

	/**
	 * 通过主键查询内部订购单配置
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询内部订购单", notes = "通过主键查询内部订购单配置，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@GetMapping("/internal/queryByIds/{ids}")
	public R<List<ConfMpInternalPurchase>> queryInternalByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confMpInternalPurchaseService.listByIds(idList));
	}

	/**
	 * 通过主键查询外部订购单配置
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询外部订购单", notes = "通过主键查询外部订购单配置，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@GetMapping("/external/queryByIds/{ids}")
	public R<List<ConfMpExternalPurchase>> queryExternalByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confMpExternalPurchaseService.listByIds(idList));
	}

	/**
	 * 分页查询内部订购单
	 * @param params 中台监控-订购单配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询内部订购单", notes = "分页查询内部订购单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true),
			@ApiImplicitParam(name = "abilityCode", value = "能力编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "abilityName", value = "能力名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceCode", value = "服务编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceName", value = "服务名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orderCode", value = "订单编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderCode", value = "子订单编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/internal/page")
	public R<Page<ConfMpInternalPurchase>> internalPage(
			@ApiIgnore @RequestBody ConfMpPurchaseParams<ConfMpInternalPurchase> params) {
		return confMpInternalPurchaseService.internalPage(params);
	}

	/**
	 * 分页查询外部订购单
	 * @param params 中台监控-订购单配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询外部订购单", notes = "分页查询外部订购单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true),
			@ApiImplicitParam(name = "abilityCode", value = "能力编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "abilityName", value = "能力名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/external/page")
	public R<Page<ConfMpExternalPurchase>> externalPage(
			@ApiIgnore @RequestBody ConfMpPurchaseParams<ConfMpExternalPurchase> params) {
		return confMpExternalPurchaseService.externalPage(params);
	}

	/**
	 * 新增内部订购单
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增内部订购单", notes = "新增内部订购单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "abilityCode", value = "能力编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "abilityName", value = "能力名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceCode", value = "服务编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceName", value = "服务名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerInsCode", value = "能力提供方机构编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerInsName", value = "能力提供方机构名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerOrgCode", value = "能力提供方单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgCode", value = "订购单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentCode", value = "订购部门编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentName", value = "订购部门名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appCode", value = "订购应用编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appName", value = "订购应用名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orderCode", value = "订单编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderCode", value = "子订单编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderType", value = "子订单类型", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderStatus", value = "子订单状态", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceGrantResult", value = "服务开通结果", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/internal/save")
	public R<Boolean> internalSave(@ApiIgnore @RequestBody ConfMpPurchaseParams<ConfMpInternalPurchase> params) {
		return confMpInternalPurchaseService.internalSave(params);
	}

	/**
	 * 新增外部订购单
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增外部订购单", notes = "新增外部订购单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "abilityCode", value = "能力编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "abilityName", value = "能力名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerOrgCode", value = "能力提供方单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerOrgName", value = "能力提供方单位名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "userOrgCode", value = "能力使用方单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "userOrgName", value = "能力使用方单位名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/external/save")
	public R<Boolean> externalSave(@ApiIgnore @RequestBody ConfMpPurchaseParams<ConfMpExternalPurchase> params) {
		return confMpExternalPurchaseService.externalSave(params);
	}

	/**
	 * 主键修改内部订购单
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键修改内部订购单", notes = "主键修改内部订购单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "abilityCode", value = "能力编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "abilityName", value = "能力名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceCode", value = "服务编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceName", value = "服务名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerInsCode", value = "能力提供方机构编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerInsName", value = "能力提供方机构名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerOrgCode", value = "能力提供方单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orgCode", value = "订购单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentCode", value = "订购部门编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "departmentName", value = "订购部门名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appCode", value = "订购应用编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "appName", value = "订购应用名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "orderCode", value = "订单编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderCode", value = "子订单编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderType", value = "子订单类型", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "suborderStatus", value = "子订单状态", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "serviceGrantResult", value = "服务开通结果", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/internal/updateById")
	public R<Boolean> internalUpdateById(@ApiIgnore @RequestBody ConfMpPurchaseParams<ConfMpInternalPurchase> params) {
		return confMpInternalPurchaseService.internalUpdateById(params);
	}

	/**
	 * 主键修改外部订购单
	 * @param params 中台监控-单位配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键修改外部订购单", notes = "主键修改外部订购单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "abilityCode", value = "能力编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "abilityName", value = "能力名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerOrgCode", value = "能力提供方单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "providerOrgName", value = "能力提供方单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "userOrgCode", value = "能力使用方单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "userOrgName", value = "能力使用方单位名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/external/updateById")
	public R<Boolean> externalUpdateById(@ApiIgnore @RequestBody ConfMpPurchaseParams<ConfMpExternalPurchase> params) {
		return confMpExternalPurchaseService.externalUpdateById(params);
	}

	/**
	 * 主键删除内部订购单
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除内部订购单", notes = "主键删除内部订购单，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/internal/removeByIds/{ids}")
	public R<Boolean> internalRemoveByIds(@PathVariable("ids") List<Integer> idList) {
		return confMpInternalPurchaseService.internalRemoveByIds(idList);
	}

	/**
	 * 主键删除外部订购单
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除外部订购单", notes = "主键删除外部订购单，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/external/removeByIds/{ids}")
	public R<Boolean> externalRemoveByIds(@PathVariable("ids") List<Integer> idList) {
		return confMpExternalPurchaseService.externalRemoveByIds(idList);
	}

	/**
	 * 导入内部订购单配置
	 * @param excelVoList 导入的数据集
	 * @param bindingResult 用来校验 实体 jsr303 校验失败结果
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导入内部订购单", notes = "通过Excel导入内部订购单配置")
	@PostMapping("/internal/import")
	public R<List<ErrorMessage>> internalImport(
			@RequestExcel(maxSheetNum = 1) List<ConfMpInternalPurchaseExcelVo> excelVoList,
			BindingResult bindingResult) {
		return confMpInternalPurchaseService.internalImport(excelVoList, bindingResult);
	}

	/**
	 * 下载内部订购单导入模板
	 * @return 模板excel文件流
	 */
	@ApiOperation(value = "下载内部订购单导入模板", notes = "下载内部订购单导入模板")
	@ResponseExcel
	@GetMapping("/internal/downloadTemplate")
	public List<ConfMpInternalPurchaseExcelVo> downloadInternalTemplate() {
		return Collections.singletonList(new ConfMpInternalPurchaseExcelVo());
	}

	/**
	 * 导入外部订购单配置
	 * @param excelVoList 导入的数据集
	 * @param bindingResult 用来校验 实体 jsr303 校验失败结果
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导入外部订购单", notes = "通过Excel导入外部订购单配置")
	@PostMapping("/external/import")
	public R<List<ErrorMessage>> externalImport(
			@RequestExcel(maxSheetNum = 1) List<ConfMpExternalPurchaseExcelVo> excelVoList,
			BindingResult bindingResult) {
		return confMpExternalPurchaseService.externalImport(excelVoList, bindingResult);
	}

	/**
	 * 下载外部订购单导入模板
	 * @return 模板excel文件流
	 */
	@ApiOperation(value = "下载外部订购单导入模板", notes = "下载外部订购单导入模板")
	@ResponseExcel
	@GetMapping("/external/downloadTemplate")
	public List<ConfMpExternalPurchaseExcelVo> downloadExternalTemplate() {
		return Collections.singletonList(new ConfMpExternalPurchaseExcelVo());
	}
}
